using System;

namespace LogikBug
{
	/// <summary>
	/// 
	/// </summary>
	public static class ObjectExtensions
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsNull(this object value)
		{
			return value == null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsNotNull(this object value)
		{
			return value != null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public static void ThrowIfNull(this object value)
		{
			value.ThrowIfNull("value");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="paramName"></param>
		public static void ThrowIfNull(this object value, string paramName)
		{
			if (value.IsNull())
			{
				throw new ArgumentNullException(paramName);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public static T ThrowIfNull<T>(this T value)
		{
			if (value.IsNull())
			{
				throw new ArgumentNullException();
			}

			return value;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="paramName"></param>
		public static T ThrowIfNull<T>(this T value, string paramName)
		{
			if (value.IsNull())
			{
				throw new ArgumentNullException(paramName);
			}

			return value;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value"></param>
		/// <returns></returns>
		public static T CastAs<T>(this object value)
		{
			try
			{
				return (T)value;
			}
			catch (Exception)
			{
				return default(T);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsDefaultValue<T>(this T value)
		{
			return Equals(value, default(T));
		}
	}
}